/**
 * delete.js
 * 1、功能：单个或多个，删除手动选中的表信息。
 
 * __ date __:2021年12月4日 16点48分
 * __ version __:V1.1.1
 * __ author __:GetHigh
 */
window.onload = function () {
//------------------------------获取所选表格------------------------------
	function getServletNameByTable(){
		var servletName="";
		var selectTable = document.getElementById("table_select");
		var index = selectTable.selectedIndex;
		var tableName = selectTable.options[index].value;
		if(tableName=="优惠劵信息"){
			servletName="coupon_DeleteServlet";
		}else if(tableName=="设备信息"){
			servletName="device_DeleteServlet";
		}else if(tableName=="商品信息"){
			servletName="goods_DeleteServlet";
		}else if(tableName=="管理员信息"){
			servletName="manager_DeleteServlet";
		}
		//console.log("servletName:"+servletName);
		return servletName;
	}
	
//------------------------删除功能-------------------------------------
	var delete_Button = document.getElementById("delete");//删除按钮
	var selectAll = document.getElementById("checkAll");//查询所有按钮
	var cancel = document.getElementById("cancel");//取消按钮
	
	
//BEGIN__批量删除功能: 隐藏按钮实现，当按下 “批量删除” 按钮 ，后多出一个删除按钮，以确定删除
	var deleteBatch = document.getElementById("deleteBatch");//批量删除按钮

	
	deleteBatch.onclick = function(){
		delete_Button.style.visibility = "visible";
		cancel.style.visibility = "visible";
		//标签的“visibility”能见度属性：当它为hidden ，表示隐藏;为visible,可见	
	}
	cancel.onclick = function(){//恢复隐藏形式：在删除之后 见下
		delete_Button.style.visibility = "hidden";
		cancel.style.visibility = "hidden";
	}
	
//END__批量删除功能
	//获取删除项Id函数: 
	function getIds(){
		var Ids="";
		var delTable = document.getElementsByName("checkbox1");
		
		for(var i=0;i<delTable.length;i++){
			if(delTable[i].checked==true){//选中的GOODS
				//console.log("delTable:"+delTable[i].value);
				Ids+=delTable[i].value+" ";
			}
		}
		return Ids; 
	}

	
	//deleteItems函数: 删除 所选项 ITEMs
	function deleteItems(Ids,name_servlet){
		var DeleteCount="";
		var servletUrl = "../"+name_servlet;
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function(){
			if(xhr.readyState==4){
				if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
					var reAjax = xhr.responseText;
					if(reAjax!="0"){
						console.log("reAjax:"+reAjax);
						DeleteCount=reAjax+"";
					}else{
						DeleteCount="0";
					}
				}else{
					console.log("删除失败！");
				}
			}
		}
		xhr.open("post",servletUrl,true);
		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
		xhr.send("Ids=K "+Ids);
		//Ajax 函数完毕
		console.log("返回DeleteCount:"+DeleteCount);
		return DeleteCount;
	}
	
	//多个或一个 删除商品功能
	delete_Button.onclick = function(){
		var Ids = getIds();//获取选中gId,拿到一个数组
		if(Ids!=0){
			//点击弹出确定框
			var dlt = confirm("您确定要删除吗？");
			//判读是，则删除；否则，返回
			if(dlt==true){
				if(Ids.length!=0){//有商品选择了
					//调用删除
					var servletName = getServletNameByTable();
					var dtgoods = deleteItems(Ids,servletName);
					console.log("dtgoods:"+dtgoods);
					//返回“删除成功！”
					if(dtgoods!="0"){
						alert("删除成功！");
					}else{
						alert("删除失败！");
					}
				}else{
					alert("请先勾选您要删除的选项!");
				}
			}
		}else{
			alert("请先勾选您要删除的选项!");
		}
	}
	
}
